package org.example.tree;

/**
 * @author liyishan
 * @date 2024/5/16 10:14
 * @apiNote
 */

public class Test00572IsSubtree {

    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        return dfs(root,subRoot);
    }

    public boolean dfs(TreeNode s,TreeNode t){
        if (s == null){
            return false;
        }
        return check(s,t) || dfs(s.left,t) || dfs(s.right,t);
    }

    public boolean check(TreeNode s,TreeNode t){
        if (s == null && t == null){
            return true;
        }
        if(s == null || t == null || s.val != t.val){
            return false;
        }
        return check(s.left,t.left) && check(s.right,t.right);
    }
}
